package cn.yunhe.action;

import cn.yunhe.model.Member;
import cn.yunhe.model.Pagination;
import cn.yunhe.service.MemberBiz;
import cn.yunhe.util.BaseActionUtil;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberAction extends BaseActionUtil implements ModelDriven<Member> {

    @Autowired
    @Qualifier(value = "memberBiz")
    private MemberBiz memberBiz;
    private Member member;
    private int currPageNo;
    private int maxRows;

    private Pagination<Member>pagination;
    private Map<String,Object>objectMap=new HashMap<>();


    public String searchMemberByConditions(){
        System.out.println("getMname===="+member.getMname());
        System.out.println("currPageNo====="+currPageNo);
        System.out.println("maxRows====="+maxRows);

        objectMap.put("mname",member.getMname());

        int totalRecords= memberBiz.searchMember2TotalRecords(objectMap);
        pagination=new Pagination<>();
        pagination.setCurrPageNo(currPageNo);
        pagination.setMaxRows(maxRows);
        pagination.setTotalRecords(totalRecords);


        System.out.println("=====================");
        System.out.println("getTotalRecords======="+pagination.getTotalRecords());
        System.out.println("getCurrPageNo======="+pagination.getCurrPageNo());
        System.out.println("getMaxRows======="+pagination.getMaxRows());
        System.out.println("getTotalPages======="+pagination.getTotalPages());
        System.out.println("getStartIndex======="+pagination.getStartIndex());
        System.out.println("=====================");

        objectMap.put("startIndex",pagination.getStartIndex());
        objectMap.put("maxRows",pagination.getMaxRows());
        List<Member>memberList= memberBiz.searchMember(objectMap);
        System.out.println("memberList========="+memberList);
        pagination.setList(memberList);

        return SUCCESS;
    }

    public MemberBiz getMemberBiz() {
        return memberBiz;
    }

    public void setMemberBiz(MemberBiz memberBiz) {
        this.memberBiz = memberBiz;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public int getCurrPageNo() {
        return currPageNo;
    }

    public void setCurrPageNo(int currPageNo) {
        this.currPageNo = currPageNo;
    }

    public int getMaxRows() {
        return maxRows;
    }

    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    public Pagination<Member> getPagination() {
        return pagination;
    }

    public void setPagination(Pagination<Member> pagination) {
        this.pagination = pagination;
    }

    public Map<String, Object> getObjectMap() {
        return objectMap;
    }

    public void setObjectMap(Map<String, Object> objectMap) {
        this.objectMap = objectMap;
    }

    @Override
    public Member getModel() {
        if (member==null){
            member=new Member();
        }
        return member;
    }
}
